package com.cn.ant.modules.score.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.modules.score.dao.SchoolLqRateDao;
import com.cn.ant.modules.score.entity.SchoolLqRate;
import com.cn.ant.modules.score.entity.SchoolTwfInfo;
import com.cn.ant.modules.sys.entity.Dict;
import com.cn.ant.modules.sys.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学校录取概率Service
 * @author 黄根华
 * @version 2015-10-11
 */
@Service
@Transactional(readOnly = true)
public class SchoolLqRateService extends BaseService {

    @Autowired
	private SchoolLqRateDao schoolLqRateDao;
	@Autowired
	private BatchLineService batchLineService;
	@Autowired
	private SchoolTwfInfoService schoolTwfInfoService;

	public SchoolLqRate get(String id) {
		return schoolLqRateDao.get(id);
	}
	
	public List<SchoolLqRate> findList(SchoolLqRate schoolLqRate) {
		return schoolLqRateDao.findList(schoolLqRate);
	}
	
	public Page<SchoolLqRate> findPage(Page<SchoolLqRate> page, SchoolLqRate schoolLqRate) {
	    schoolLqRate.setPage(page);
        page.setList(schoolLqRateDao.findList(schoolLqRate));
        return page;
	}

	/**
	 * 查询学校的录取概率
	 * @param params
	 * @return
	 */
	public List<SchoolLqRate> query(Map<String,Object> params) {
		if (StringUtils.isNotBlank((String)params.get("majorParentCode"))) {
			return schoolLqRateDao.query(params);
		} else {
			return schoolLqRateDao.queryWithOutMajor(params);
		}
	}

	public double queryRate(String province, String schoolCode, String schoolCodeSn, String kemuType, String batchLevel, int score, int year) {
		Map<String, Object> params = new HashMap<>();
		params.put("province", province);
		params.put("schoolCode", schoolCode);
		params.put("schoolCodeSn", schoolCodeSn);
		params.put("kemuType", kemuType);
		params.put("batchLevel", batchLevel);
		params.put("score", score);
		params.put("year", year);
		double rate = queryRate(params);
		return rate * 100;
	}

	public double queryRate(Map<String, Object> params) {
		Double rate = schoolLqRateDao.queryRate(params);
		if (rate == null) {
			return 0;
		} else {
			return rate.doubleValue();
		}
	}


	@Transactional(readOnly = false)
	public void save(SchoolLqRate schoolLqRate) {
	    if (schoolLqRate.getIsNewRecord()) {
            schoolLqRate.preInsert();
            schoolLqRateDao.insert(schoolLqRate);
        } else {
            schoolLqRate.preUpdate();
            schoolLqRateDao.update(schoolLqRate);
        }
	}

	@Transactional(readOnly = false)
	public int batchSave(List<SchoolLqRate> list) {
		return schoolLqRateDao.batchInsert(list);
	}
	
	@Transactional(readOnly = false)
	public void delete(SchoolLqRate schoolLqRate) {
		schoolLqRateDao.delete(schoolLqRate);
	}

	/**
	 * 初始化指定年份的各个分数的录取概率
	 * @param year
	 */
	@Transactional(readOnly = false)
	public void initLqRate(int year,String province,String provinceName) {
		logger.info("删除现有的数据。。。");
		schoolLqRateDao.deleteByYear(year, province);
		List<Dict> kemuTypeList = DictUtils.getDictList("kemu_type");
		List<Dict> batchLineList = DictUtils.getDictList("batch_level");

		if (kemuTypeList != null && kemuTypeList.size() > 0 && batchLineList != null && batchLineList.size() > 0) {
			for (int i = 0; i < kemuTypeList.size(); i++) {
				for (int j = 0; j < batchLineList.size(); j++) {
					Dict kemu = kemuTypeList.get(i);
					Dict batchLine = batchLineList.get(j);
					int batchLineScore = batchLineService.getBatchLineScore(year, province, kemu.getValue(), batchLine.getValue());
					if (batchLineScore == 0) {
						continue;
					}
					//该批次线以上的分数全部进行概率预算
					SchoolTwfInfo param = new SchoolTwfInfo();
					param.setProvince(province);
					param.setBatchLevel(batchLine.getValue());
					param.setKemuType(kemu.getValue());
					param.setYear(year);
					List<SchoolTwfInfo> twfInfoList = schoolTwfInfoService.findList(param);
					if (twfInfoList != null && twfInfoList.size() > 0) {
						for (int k = batchLineScore; k <= 750; k++) {
							List<SchoolLqRate> rateList = new ArrayList<>();
							for (int l = 0; l < twfInfoList.size(); l++) {
								SchoolTwfInfo twfInfo = twfInfoList.get(l);
								SchoolLqRate rate = new SchoolLqRate();
								rate.setProvince(province);
								rate.setProvinceName(provinceName);
								rate.setKemuType(kemu.getValue());
								rate.setBatchLevel(batchLine.getValue());
								rate.setSchoolCode(twfInfo.getSchoolCode());
								rate.setSchoolCodeSn(twfInfo.getSchoolCodeSn());
								rate.setSchoolName(twfInfo.getSchoolName());
								rate.setYear(year);
								rate.setScore(k);
								//开始计算概率
								rate.setRate(getRate(k, twfInfo));
								rateList.add(rate);
							}
							//批量添加录取概率
							schoolLqRateDao.batchInsert(rateList);
						}
					}
				}
			}
		}
	}

	/**
	 * 计算概率
	 * @param score
	 * @param twfInfo
	 */
	public double getRate(int score, SchoolTwfInfo twfInfo) {
		double rate = 0;
		if (score >= twfInfo.getMax1()) {
			rate =  0.99;
		}else if (score < twfInfo.getMin1()) {
			if (twfInfo.getMin1() - score <= 10) {
				if ((10 + score - twfInfo.getMin1()) < (twfInfo.getMin2() - score)) {
					rate = (10 + score - twfInfo.getMin1()) / (twfInfo.getMin2() - score) * 0.1;
				} else {
					rate = 0.1;
				}

			}else if (twfInfo.getMin1() - score < 20) {
				if ((20 + score - twfInfo.getMin1()) <= (twfInfo.getMin2() - score)) {
					rate = (20 + score - twfInfo.getMin1()) / (twfInfo.getMin2() - score) * 0.05;
				}else{
					rate = 0.05;
				}
			} else {
				rate = 0.01;
			}
		}else if ((score >= twfInfo.getAvg3()) && score < twfInfo.getMax1()) {
			rate = 0.7 + (score - twfInfo.getAvg3()) / (twfInfo.getMax1() - twfInfo.getAvg3()) * 0.3;
		}else if (score >= twfInfo.getAvg2() && score < twfInfo.getAvg3()) {
			rate = 0.6 + (score - twfInfo.getAvg2()) / (twfInfo.getAvg3() - twfInfo.getAvg2()) * 0.1;
		}else if (score >= twfInfo.getAvg1() && score < twfInfo.getAvg2()) {
			rate = 0.5 + (score - twfInfo.getAvg1()) / (twfInfo.getAvg2() - twfInfo.getAvg1()) * 0.1;
		}else if (score >= twfInfo.getMin1() && score < twfInfo.getMin2()) {
			if (score > twfInfo.getMin1() && (twfInfo.getMin2() - twfInfo.getMin1() > 20)) {
				rate = (score - twfInfo.getMin1()) / (twfInfo.getMin2() - twfInfo.getMin1()) * 0.1;
			} else {
				rate = 0.1 + (score - twfInfo.getMin1()) / (twfInfo.getMin2() - twfInfo.getMin1()) * 0.1;
			}

		}else if (score >= twfInfo.getMin2() && score < twfInfo.getMin3()) {
			rate = 0.2 + (score - twfInfo.getMin2()) / (twfInfo.getMin3() - twfInfo.getMin2()) * 0.1;
		}else if (score >= twfInfo.getMin3() && score < twfInfo.getAvg1()) {
			rate = 0.3 + (score - twfInfo.getMin3()) / (twfInfo.getAvg1() - twfInfo.getMin3()) * 0.2;
		} else if (score > twfInfo.getAvg3() && score - twfInfo.getAvg3() >= 20) {
			rate = 0.99;
		} else {
			rate = 0;
		}
		return rate;
	}
	
}